package com.example.demo_01.service.impl;

import com.example.demo_01.mapper.RoomMapper;
import com.example.demo_01.service.RoomService;
import com.example.demo_01.util.ResultBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Override
    public ResultBean findByParam(Map<String, String> param) {
        int pageNum = Integer.parseInt(param.get("pageNum"));
        int pageSize = Integer.parseInt(param.get("pageSize"));
        PageHelper.startPage(pageNum, pageSize);
        List<Map> list = roomMapper.findByParam(param);
        if (list != null && list.size() > 0) {
            PageInfo<Map> pageInfo = new PageInfo<>(list);
            return ResultBean.getInstance().success().setData(pageInfo);
        }
        return ResultBean.getInstance().fail("查无数据");
    }

    @Override
    public ResultBean doSave(Map<String, String> param) {
        int row = roomMapper.save(param);
        if (row > 0) {
            return ResultBean.getInstance().success("添加教室成功");
        }
        return ResultBean.getInstance().fail("添加教室失败");
    }

    @Override
    public ResultBean doUpdate(Map<String, String> param) {
        int row = roomMapper.update(param);
        if (row > 0) {
            return ResultBean.getInstance().success("更新教室信息成功");
        }
        return ResultBean.getInstance().fail("更新教室信息失败");
    }

    @Override
    public ResultBean delete(String ids) {
        int row = roomMapper.delete(ids);
        if (row > 0) {
            return ResultBean.getInstance().success("删除教室成功");
        }
        return ResultBean.getInstance().fail("删除教室失败");
    }
}
